#!/bin/bash

function set_clipboard() {
    b=$(echo -ne "$2" | base64);
    printf "\033]52;$1;$b\a\n"
}

function test_clipboard() {
    echo -e "\033[1m ⎆ The Clipboard should now contain '$2'\033[0m"

    set_clipboard "$1" "$2"

    read -p "    ⎆ Press enter to continue " -n1 -s
    echo
    echo
}

test_clipboard "c" "copypasta!"
test_clipboard "p" "Mask: p"
test_clipboard "q" "Mask: q"
test_clipboard "s" "Mask: s"
test_clipboard "cpqs" "Mask: cpqs"

echo -e "\033[1m ⎆ The Clipboard should now be empty\033[0m"
printf "\033]52;cpqs;\a\n"

read -p "    ⎆ Press enter to continue " -n1 -s
echo
echo

echo -e "\033[1m ⎆ The Clipboard should still be empty\033[0m"
set_clipboard "01234567" "Hello World!"

read -p "    ⎆ Press enter to continue " -n1 -s
echo
echo

test_clipboard "c01234567" "Hello World!"

echo -e "\033[1m ⎆ The Clipboard should now contain the source of terminalsurface.cpp\033[0m"
printf "\033]52;c;Ly8gQ29weXJpZ2h0IChDKSAyMDIyIFRoZSBRdCBDb21wYW55IEx0ZC4KLy8gU1BEWC1MaWNlbnNlLUlkZW50aWZpZXI6IExpY2Vuc2VSZWYtUXQtQ29tbWVyY2lhbCBPUiBHUEwtMy4wKyBPUiBHUEwtMy4wIFdJVEggUXQtR1BMLWV4Y2VwdGlvbi0xLjAKCiNpbmNsdWRlICJ0ZXJtaW5hbHN1cmZhY2UuaCIKI2luY2x1ZGUgInN1cmZhY2VpbnRlZ3JhdGlvbi5oIgoKI2luY2x1ZGUgImtleXMuaCIKI2luY2x1ZGUgInNjcm9sbGJhY2suaCIKCiNpbmNsdWRlIDx2dGVybS5oPgoKI2luY2x1ZGUgPFFMb2dnaW5nQ2F0ZWdvcnk+CiNpbmNsdWRlIDxRVGltZXI+CgpuYW1lc3BhY2UgVGVybWluYWxTb2x1dGlvbiB7CgpRX0xPR0dJTkdfQ0FURUdPUlkobG9nLCAicXRjLnRlcm1pbmFsLnN1cmZhY2UiLCBRdFdhcm5pbmdNc2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CiAgICAgICAgICAgIGlmICghKG1hc2sgJiAweEYpKQogICAgICAgICAgICAgICAgcmV0dXJuIDA7CgogICAgICAgICAgICBhdXRvIHAgPSBzdGF0aWNfY2FzdDxUZXJtaW5hbFN1cmZhY2VQcml2YXRlICo+KHVzZXIpOwogICAgICAgICAgICBpZiAocC0+bV9zdXJmYWNlSW50ZWdyYXRpb24pCiAgICAgICAgICAgICAgICBwLT5tX3N1cmZhY2VJbnRlZ3JhdGlvbi0+b25HZXRDbGlwYm9hcmQoKTsKCiAgICAgICAgICAgIHJldHVybiAwOwogICAgICAgIH07CgogICAgICAgIG1fdnRlcm1TZWxlY3Rpb25DYWxsYmFja3Muc2V0ID0KICAgICAgICAgICAgW10oVlRlcm1TZWxlY3Rpb25NYXNrIG1hc2ssIFZUZXJtU3RyaW5nRnJhZ21lbnQgZnJhZywgdm9pZCAqdXNlcikgewogICAgICAgICAgICAgICAgaWYgKCEobWFzayAmIDB4RikpCiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIDA7CgogICAgICAgICAgICAgICAgYXV0byBwID0gc3RhdGljX2Nhc3Q8VGVybWluYWxTdXJmYWNlUHJpdmF0ZSAqPih1c2VyKTsKICAgICAgICAgICAgICAgIGlmIChmcmFnLmluaXRpYWwpCiAgICAgICAgICAgICAgICAgICAgcC0+bV9zZWxlY3Rpb25CdWZmZXIuY2xlYXIoKTsKCiAgICAgICAgICAgICAgICBwLT5tX3NlbGVjdGlvbkJ1ZmZlci5hcHBlbmQoZnJhZy5zdHIsIGZyYWcubGVuKTsKICAgICAgICAgICAgICAgIGlmICghZnJhZy5maW5hbCkKICAgICAgICAgICAgICAgICAgICByZXR1cm4gMTsKCiAgICAgICAgICAgICAgICBpZiAocC0+bV9zdXJmYWNlSW50ZWdyYXRpb24pCiAgICAgICAgICAgICAgICAgICAgcC0+bV9zdXJmYWNlSW50ZWdyYXRpb24tPm9uU2V0Q2xpcGJvYXJkKHAtPm1fc2VsZWN0aW9uQnVmZmVyKTsKCiA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WYXJpYW50Q29sb3IoY29uc3QgVlRlcm1Db2xvciAmY29sb3IpCiAgICB7CiAgICAgICAgaWYgKGNvbG9yLnR5cGUgJiBWVEVSTV9DT0xPUl9ERUZBVUxUX0JHKQogICAgICAgICAgICByZXR1cm4gQ29sb3JJbmRleDo6QmFja2dyb3VuZDsKICAgICAgICBlbHNlIGlmIChjb2xvci50eXBlICYgVlRFUk1fQ09MT1JfREVGQVVMVF9GRykKICAgICAgICAgICAgcmV0dXJuIENvbG9ySW5kZXg6OkZvcmVncm91bmQ7CiAgICAgICAgZWxzZSBpZiAoY29sb3IudHlwZSAmIFZURVJNX0NPTE9SX0lOREVYRUQpIHsKICAgICAgICAgICAgaWYgKGNvbG9yLmluZGV4ZWQuaWR4ID49IDE2KSB7CiAgICAgICAgICAgICAgICBWVGVybUNvbG9yIGMgPSBjb2xvcjsKICAgICAgICAgICAgICAgIHZ0ZXJtX3N0YXRlX2NvbnZlcnRfY29sb3JfdG9fcmdiKHZ0ZXJtX29idGFpbl9zdGF0ZShtX3Z0ZXJtLmdldCgpKSwgJmMpOwogICAgICAgICAgICAgICAgcmV0dXJuIHRvUUNvbG9yKGMpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBjb2xvci5pbmRleGVkLmlkeDsKICAgICAgICB9IGVsc2UgaWYgKGNvbG9yLnR5cGUgPT0gVlRFUk1fQ09MT1JfUkdCKQogICAgICAgICAgICByZXR1cm4gdG9RQ29sb3IoY29sb3IpOwogICAgICAgIGVsc2UKICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgfQoKICAgIFRlcm1pbmFsQ2VsbCB0b0NlbGwoY29uc3QgVlRlcm1TY3JlZW5DZWxsICZjZWxsKQogICAgewogICAgICAgIFRlcm1pbmFsQ2VsbCByZXN1bHQ7CiAgICAgICAgcmVzdWx0LndpZHRoID0gY2VsbC53aWR0aDsKICAgICAgICByZXN1bHQudGV4dCA9IFFTdHJpbmc6OmZyb21VY3M0KGNlbGwuY2hhcnMp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Nhc2UgVlRFUk1fVU5ERVJMSU5FX0NVUkxZOgogICAgICAgICAgICAgICAgcmVzdWx0LnVuZGVybGluZVN0eWxlID0gUVRleHRDaGFyRm9ybWF0OjpXYXZlVW5kZXJsaW5lOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgVlRFUk1fVU5ERVJMSU5FX0RBU0hFRDoKICAgICAgICAgICAgICAgIHJlc3VsdC51bmRlcmxpbmVTdHlsZSA9IFFUZXh0Q2hhckZvcm1hdDo6RGFzaFVuZGVybGluZTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIFZURVJNX1VOREVSTElORV9ET1RURUQ6CiAgICAgICAgICAgICAgICByZXN1bHQudW5kZXJsaW5lU3R5bGUgPSBRVGV4dENoYXJGb3JtYXQ6OkRvdExpbmU7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgcmVzdWx0LnN0cmlrZU91dCA9IGNlbGwuYXR0cnMuc3RyaWtlOwoKICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgfQoKICAgIC8vIENhbGxiYWNrcyBmcm9tIHZ0ZXJtCiAgICB2b2lkIGludmFsaWRhdGUoVlRlcm1SZWN0IHJlY3QpCiAgICB7CiAgICAgICAgaWYgKCFtX2FsdHNjcmVlbikgewogICAgICAgICAgICByZWN0LnN0YXJ0X3JvdyArPSBtX3Njcm9sbGJhY2stPnNpemUoKTsKICAgICAgICAgICAgcmVjdC5lbmRfcm93ICs9IG1fc2Nyb2xsYmFjay0+c2l6ZSgpOwogICAgICAgIH0KCiAgICAgICAgZW1pdCBxLT5pbnZhbGlkYXRlZCgKICAgICAgICAgICAgUVJlY3R7UVBvaW50e3JlY3Quc3RhcnRfY29sLCByZWN0LnN0YXJ0X3Jvd30sIFFQb2ludHtyZWN0LmVuZF9jb2wsIHJlY3QuZW5kX3JvdyAtIDF9fSk7CiAgICB9CgogICAgaW50IHNiX3B1c2hsaW5lKGludCBjb2xzLCBjb25zdCBWVGVybVNjcmVlbkNlbGwgKmNlbGxzKQogICAgewogICAgICAgIG1fc2Nyb2xsYmFjay0+ZW1wbGFjZShjb2xzLCBjZWxscyk7CiAgICAgICAgZW1pdCBxLT5mdWxsU2l6ZUNoYW5nZWQocS0+ZnVsbFNpemUoKSk7CiAgICAgICAgcmV0dXJuIDE7CiAgICB9CgogICAgaW50IHNiX3BvcGxpbmUoaW50IGNvbHMsIFZUZXJtU2NyZWVuQ2VsbCAqY2VsbHMpCiAgICB7CiAgICAgICAgaWYgKG1fc2Nyb2xsYmFjay0+c2l6ZSgpID09IDApCiAgICAgICAgICAgIHJldHVybiAwOwoKICAgICAgICBtX3Njcm9sbGJhY2stPnBvcHRvKGNvbHMsIGNlbGxzKTsKICAgICAgICBlbWl0IHEtPmZ1bGxTaXplQ2hhbmdlZChxLT5mdWxsU2l6ZSgpKTsKICAgICAgICByZXR1cm4gMTsKICAgIH0KCiAgICBpbnQgc2JfY2xlYXIoKQogICAgewogICAgICAgIG1fc2Nyb2xsYmFjay0+Y2xlYXIoKTsKICAgICAgICBlbWl0IHEtPmZ1bGxTaXplQ2hhbmdlZChxLT5mdWxsU2l6ZSgpKTsKICAgICAgICByZXR1cm4gMTsKICAgIH0KCiAgICBpbnQgb3NjKGludCBjbWQsIGNvbnN0IFZUZXJtU3RyaW5nRnJhZ21lbnQgJmZyYWdtZW50KQogICAgewogICAgICAgIGlmIChtX3N1cmZhY2VJbnRlZ3JhdGlvbikgewogICAgICAgICAgICBtX3N1cmZhY2VJbnRlZ3JhdGlvbi0+b25Pc2MoY21kLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAge2ZyYWdtZW50LnN0ciwgZnJhZ21lbnQubGVufSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyYWdtZW50LmluaXRpYWwsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcmFnbWVudC5maW5hbCk7CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gMTsKICAgIH0KCiAgICBpbnQgc2V0VGVybWluYWxQcm9wZXJ0aWVzKFZUZXJtUHJvcCBwcm9wLCBWVGVybVZhbHVlICp2YWwpCiAgICB7CiAgICAgICAgc3dpdGNoIChwcm9wKSB7CiAgICAgICAgY2FzZSBWVEVSTV9QUk9QX0NVUlNPUlZJU0lCTEU6IHsKICAgICAgICAgICAgQ3Vyc29yIG9sZCA9IHEtPmN1cnNvcigpOwogICAgICAgICAgICBtX2N1cnNvci52aXNpYmxlID0gdmFsLT5ib29sZWFuOwogICAgICAgICAgICBxLT5jdXJzb3JDaGFuZ2VkKG9sZCwgcS0+Y3Vyc29yKCkpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgY2FzZSBWVEVSTV9QUk9QX0NVUlNPUkJMSU5LOiB7CiAgICAgICAgICAgIEN1cnNvciBvbGQgPSBxLT5jdXJzb3IoKTsKICAgICAgICAgICAgbV9jdXJzb3IuYmxpbmsgPSB2YWwtPmJvb2xlYW47CiAgICAgICAgICAgIGVtaXQgcS0+Y3Vyc29yQ2hhbmdlZChvbGQsIHEtPmN1cnNvcigpKTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgICAgIGNhc2UgVlRFUk1fUFJPUF9DVVJTT1JTSEFQRTogewogICAgICAgICAgICBDdXJzb3Igb2xkID0gcS0+Y3Vyc29yKCk7CiAgICAgICAgICAgIG1fY3Vyc29yLnNoYXBlID0gKEN1cnNvcjo6U2hhcGUpIHZhbC0+bnVtYmVyOwogICAgICAgICAgICBlbWl0IHEtPmN1cnNvckNoYW5nZWQob2xkLCBxLT5jdXJzb3IoKSk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBjYXNlIFZURVJNX1BST1BfSUNPTk5BTUU6CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGNhc2UgVlRFUk1fUFJPUF9USVRMRToKICA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M0KCk7CiAgICByZXR1cm4gc3RkOjp1MzJzdHJpbmcodWNzNC5iZWdpbigpLCB1Y3M0LmVuZCgpKS5mcm9udCgpOwp9CgpUZXJtaW5hbENlbGwgVGVybWluYWxTdXJmYWNlOjpmZXRjaENlbGwoaW50IHgsIGludCB5KSBjb25zdAp7CiAgICBzdGF0aWMgVGVybWluYWxDZWxsIGVtcHR5Q2VsbHsxLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAge30sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7fSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZhbHNlLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgQ29sb3JJbmRleDo6Rm9yZWdyb3VuZCwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIENvbG9ySW5kZXg6OkJhY2tncm91bmQsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBRVGV4dENoYXJGb3JtYXQ6Ok5vVW5kZXJsaW5lLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZmFsc2V9OwoKICAgIGlmICh5IDwgMCB8fCB5ID49IGZ1bGxTaXplKCkuaGVpZ2h0KCkgfHwgeCA+PSBmdWxsU2l6ZSgpLndpZHRoKCkpIHsKICAgICAgICBxQ1dhcm5pbmcobG9nKSA8PCAiSW52YWxpZCBQYXJhbWV0ZXIgZm9yIGZldGNoQ2VsbDoiIDw8IHggPDwgeSA8PCAiZnVsbFNpemU6IiA8PCBmdWxsU2l6ZSgpOwogICAgICAgIHJldHVybiBlbXB0eUNlbGw7CiAgICB9CgogICAgY29uc3QgVlRlcm1TY3JlZW5DZWxsICpyZWZDZWxsID0gZC0+Y2VsbEF0KHgsIHkpOwogICAgaWYgKCFyZWZDZWxsKQogICAgICAgIHJldHVybiBlbXB0eUNlbGw7CgogICAgcmV0dXJuIGQtPnRvQ2VsbCgqcmVmQ2VsbCk7Cn0KCnZvaWQgVGVybWlu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